﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using System.Web.Services;
using Emoticons;

namespace Ajax_GiuaKy
{
    public partial class _Default : System.Web.UI.Page
    {
        public static string user_email = "";
        public static string user_avatar = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Emoticon[] List_Emoticons = Emoticon.All;
            
            var openid = new OpenIdRelyingParty();
            var response = openid.GetResponse();
            if (response != null && response.Status == AuthenticationStatus.Authenticated)
            {
                var fetch = response.GetExtension<FetchResponse>();
                if (fetch != null)
                {
                    user_email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                    var pre_email = user_email.Substring(0, user_email.IndexOf("@"));
                    user_email = pre_email;
                    user_avatar = "http://img.msg.yahoo.com/avatar.php?yids=email&format=gif".Replace("email", pre_email);
                }
                FormsAuthentication.RedirectFromLoginPage(response.ClaimedIdentifier, false);
            }
        }

        protected void btnGoogle_Click(object sender, ImageClickEventArgs e)
        {
            getAuthentication("https://www.google.com/accounts/o8/id");
        }

        protected void btnYahoo_Click(object sender, ImageClickEventArgs e)
        {
            getAuthentication("https://me.yahoo.com");
        }

        protected void btnFacebook_Click(object sender, ImageClickEventArgs e)
        {
            //getAuthentication("https://register.facebook.com/editaccount.php?ref=mb&drop");
            getAuthentication("http://www.facebook.com/advertising/?connect");
            
        }
        private void getAuthentication(string url)
        {
            using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
            {
                //var request = openid.CreateRequest("https://me.yahoo.com");
                //var request = openid.CreateRequest("https://www.google.com/accounts/o8/id");
                var request = openid.CreateRequest(url);
                var fetch = new FetchRequest();
                fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            
                request.AddExtension(fetch);
                request.RedirectToProvider();
            }
            
        }
        [System.Web.Services.WebMethod()]
        public static string getHello(string str)
        {
            str = Emoticon.Format(str);   
            return str;
        }
    }
}
